package com.samapp.map;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.samapp.MyApplication;
import com.samapp.R;
import com.samapp.db.SamappDB;
import com.samapp.entity.Client;

public class ClientDistributionActivity extends Activity implements OnClickListener {

	private MapView mMapView = null;
	private Button btnBack = null;
	private Button btnList = null;
	private Button btnDelete = null;
	private Button btnSearch = null;
	private EditText editTextSearch = null;
	private BaiduMap baiduMap;
	/** Marker图标 */
	private BitmapDescriptor bitmap;
	/** 地图中心点 */
//	private LatLng cenpt = new LatLng(23.132531, 113.376261);
	/** 标注数组 */
	private List<Marker> markers = new ArrayList<Marker>();
	
	private MyApplication myApplication = MyApplication.getApplicationInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 在使用SDK各组件之前传入context信息初始化
		// 注意该方法要在setContentView方法之前实现
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.client_distribution);
		init();
	}

	private void init() {
		initView();
		initBaiduMap();
		initListener();
	}

	private void initView() {
		mMapView = (MapView) findViewById(R.id.bmapView);
		btnBack = (Button) findViewById(R.id.btn_back);
		btnList = (Button) findViewById(R.id.btn_list);
		btnDelete = (Button) findViewById(R.id.btn_delete);
		btnSearch = (Button) findViewById(R.id.btn_search);
		editTextSearch = (EditText) findViewById(R.id.edit_search);
		editTextSearch.setHint("按客户名称模糊搜索");
	}

	/**
	 * 初始化百度地图
	 */
	private void initBaiduMap() {
		baiduMap = mMapView.getMap();
		baiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(15).build()));
		if(myApplication.curCenPT.longitude > 0 && myApplication.curCenPT.latitude > 0){
			MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newLatLng(myApplication.curCenPT);
			baiduMap.animateMapStatus(mMapStatusUpdate);
		}
		bitmap = BitmapDescriptorFactory.fromResource(R.drawable.marker);
		initClientLocation();
		// 对marker添加点击相应事件
		baiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

			@Override
			public boolean onMarkerClick(Marker clickMarker) {
				String Fid = clickMarker.getTitle();
				myApplication.curClientFid = Fid;
				skip(ViewClientActivity.class);
				finish();
//				Toast.makeText(getApplicationContext(), clickMarker.getTitle(), Toast.LENGTH_SHORT).show();
				return false;
			}
		});
	}

	private void initClientLocation() {
		SamappDB samappDB = new SamappDB(ClientDistributionActivity.this, MyApplication.getApplicationInstance().databaseVesion);
		Cursor cursor = samappDB.select();
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			Client client = new Client(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15));
			myApplication.clientsMap.put(cursor.getString(0), client);
			addOverlay(client);
			
			if(i == 0 && myApplication.curCenPT.longitude == 0 && myApplication.curCenPT.latitude == 0){
				myApplication.curCenPT = new LatLng(Double.parseDouble(client.getLatitude()), Double.parseDouble(client.getLongitude()));
				MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newLatLng(myApplication.curCenPT);
				baiduMap.animateMapStatus(mMapStatusUpdate);
			}
		}
	}

	private void addOverlay(Client client) {
		LatLng mLatLng = new LatLng(Double.parseDouble(client.getLatitude()), Double.parseDouble(client.getLongitude()));
		MarkerOptions markerOptions = new MarkerOptions().icon(bitmap).position(mLatLng);
		Marker marker = (Marker) baiduMap.addOverlay(markerOptions);
		marker.setTitle(client.getFid());
		markers.add(marker);
	}

	private void initListener() {
		btnBack.setOnClickListener(this);
		btnList.setOnClickListener(this);
		btnDelete.setOnClickListener(this);
		btnSearch.setOnClickListener(this);
	}

	// 获取焦点
	@Override
	protected void onResume() {
		super.onResume();
		mMapView.onResume();
	}

	// 失去焦点
	@Override
	protected void onPause() {
		super.onPause();
		mMapView.onPause();
	}

	// 销毁
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mMapView.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_list:
			skip(ClientListActivity.class);
			finish();
			break;
		case R.id.btn_delete:
			editTextSearch.setText("");
			break;
		case R.id.btn_search:
			stopInput();
			String clientName = editTextSearch.getText().toString().trim();
			boolean isExist = false;
			for (Marker marker0 : markers) {
				if (clientName.equals(marker0.getTitle())) {
					MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newLatLng(marker0.getPosition());
					baiduMap.animateMapStatus(mMapStatusUpdate);
					isExist = true;
					break;
				}
			}
			if (!isExist) {
				Toast.makeText(ClientDistributionActivity.this, "没有这个客户", Toast.LENGTH_SHORT).show();
			}
			break;
		}
	}

	/**
	 * 隐藏输入法
	 */
	private void stopInput() {
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMethodManager.hideSoftInputFromWindow(ClientDistributionActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}
	
	/** 跳转页面 */
	private void skip(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
	}

	/** 修改返回键 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		btnBack.performClick();
	}
}
